//
//  ZYLoginView.m
//  YiLin
//
//  Created by Zyark on 2018/5/29.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYLoginView.h"
#import "WXApi.h"

@interface ZYLoginView()
@property(nonatomic,strong) UIImageView *logoImageView;
@property(nonatomic,strong) UITextField *usernameTextField;
@property(nonatomic,strong) UITextField *passwordTextField;
@property(nonatomic,strong) UIButton *loginBtn;
@property(nonatomic,strong) UIButton *forgetPasswordBtn;
@property(nonatomic,strong) UIButton *weChatLoginBtn;
@end

@implementation ZYLoginView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}

-(void)setUp{
    UIImage *logo = [UIImage imageNamed:@"ic_login_logo"];
    UIImageView *logoImageView = [[UIImageView alloc] initWithImage:logo];
    UITextField *usernameTextField = [[UITextField alloc] init];
    UITextField *passwordTextField = [[UITextField alloc] init];
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *forgetPasswordBtn = [[UIButton alloc] init];
    UIImage *weChatImage = [UIImage imageNamed:@"ic_login_wechat_btn"];
    UIButton *weChatLoginBtn = [[UIButton alloc] init];
    CGFloat left = ZYSCREEN_W * 0.08;
    CGFloat height = ZYSCREEN_W * 0.1;
    CGFloat padding = ZYSCREEN_W * 0.1;
    CGFloat logoWidth = ZYSCREEN_W * 0.24;
    
    [weChatLoginBtn setBackgroundImage:weChatImage forState:UIControlStateNormal];
    //设置渐变图片
    [ZYUtils setGradualChangingColorWithButton:loginBtn size:CGSizeMake(self.width - left * 2, height)];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    loginBtn.layer.cornerRadius = 5;
    loginBtn.layer.masksToBounds = YES;
    
    [forgetPasswordBtn setTitleColor:kBtnTitleColor forState:UIControlStateNormal];
    [forgetPasswordBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
    forgetPasswordBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    
    //按钮点击
    [loginBtn addTarget:self action:@selector(doLogin:) forControlEvents:UIControlEventTouchUpInside];
    [forgetPasswordBtn addTarget:self action:@selector(doForgetPassword:) forControlEvents:UIControlEventTouchUpInside];
    [weChatLoginBtn addTarget:self action:@selector(doWeChatLogin:) forControlEvents:UIControlEventTouchUpInside];
    
    //初始化textField
    [passwordTextField setSize:CGSizeMake(self.width - left * 2, height)];
    [usernameTextField setSize:CGSizeMake(self.width - left * 2, height)];
    
    passwordTextField.secureTextEntry = YES;
    usernameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    
    [self addSubview:logoImageView];
    [self addSubview:usernameTextField];
    [self addSubview:passwordTextField];
    [self addSubview:loginBtn];
    [self addSubview:forgetPasswordBtn];
    [self addSubview:weChatLoginBtn];
    
    
    ZYWeakSelf(weakSelf)
    [logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.centerX.mas_equalTo(weakSelf.mas_centerX);
        make.height.mas_equalTo(logo.size.height);
        make.width.mas_equalTo(logo.size.width);
    }];
    
    [usernameTextField  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakSelf.mas_centerX);
        make.left.mas_equalTo(left);
        make.top.mas_equalTo(logoImageView.mas_bottom).offset(padding * 2);
        make.height.mas_equalTo(height);
    }];
    
    [passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakSelf.mas_centerX);
        make.left.mas_equalTo(left);
        make.top.mas_equalTo(usernameTextField.mas_bottom).offset(padding * 0.5);
        make.height.mas_equalTo(height);
    }];
    
    
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakSelf.mas_centerX);
        make.left.mas_equalTo(left);
        make.top.mas_equalTo(passwordTextField.mas_bottom).offset(padding);
        make.height.mas_equalTo(height);
    }];
    
    [forgetPasswordBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(loginBtn);
        make.top.mas_equalTo(loginBtn.mas_bottom).offset(8);
        make.height.mas_equalTo(25);
        make.width.mas_equalTo(120);
    }];
    
    [weChatLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakSelf.mas_centerX);
        make.top.mas_equalTo(loginBtn.mas_bottom).offset(padding);
    }];
    
    if (![WXApi isWXAppInstalled]) {
        //判断是否有微信
        weChatLoginBtn.hidden = YES;
    }else{
        weChatLoginBtn.hidden = NO;
    }
    
    _loginBtn = loginBtn;
    _logoImageView = logoImageView;
    _usernameTextField = usernameTextField;
    _passwordTextField = passwordTextField;
    _weChatLoginBtn = weChatLoginBtn;
    _forgetPasswordBtn = forgetPasswordBtn;
    
    
    [self setLeftImageView:usernameTextField imageName:@"ic_login_username_left"];
    [self setLeftImageView:passwordTextField imageName:@"ic_login_password_left"];
    [self setTextFieldLine:usernameTextField];
    [self setTextFieldLine:passwordTextField];
    
    [self setupTitle];//初始化标题
}


/**
 设置左视图
 @param textField texField
 @param imageName 显示的图片
 */
-(void)setLeftImageView:(UITextField *)textField imageName:(NSString *)imageName{
    UIImageView *leftImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    leftImageView.frame = CGRectMake(0, 0, 17, 21);
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ZYSCREEN_W * 0.08, ZYSCREEN_W * 0.26)];
    [leftView addSubview:leftImageView];
    leftImageView.centerY = leftView.centerY;
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.leftView = leftView;
}


/**
 初始化标题
 */
-(void)setupTitle{
    NSString *usernamePlaceholder = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_LOGIN_USERNAME_PLACEHOLDER];
    NSString *passwordPlaceholder = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_LOGIN_PASSWORD_PLACEHOLDER];
    NSString *loginBtnTitle = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_LOGIN_LOGIN_BTN_TITLE];
    NSString *forgetPasswordBtnTitle = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_LOGIN_FORGET_PASSWORD_BTN_TITLE];
    [_loginBtn setTitle:loginBtnTitle forState:UIControlStateNormal];
    [_forgetPasswordBtn setTitle:forgetPasswordBtnTitle forState:UIControlStateNormal];
    _usernameTextField.placeholder = usernamePlaceholder;
    _passwordTextField.placeholder = passwordPlaceholder;
}

-(void)setTextFieldLine:(UITextField *)textField{
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = RGBA(235, 235, 235, 1);
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(textField);
        make.right.equalTo(textField);
        make.top.equalTo(textField.mas_bottom);
        make.height.equalTo(@1);
    }];
}

#pragma mark - Event
-(void)doLogin:(id)button{
    if ([self.delegate respondsToSelector:@selector(loginView:doLogin:)]) {
        [self.delegate loginView:self doLogin:button];
    }
}

-(void)doForgetPassword:(id)button{
    if ([self.delegate respondsToSelector:@selector(loginView:doForgetPassword:)]) {
        [self.delegate loginView:self doForgetPassword:button];
    }
}

-(void)doWeChatLogin:(id)button{
    if ([self.delegate respondsToSelector:@selector(loginView:doWeChatLogin:)]) {
        [self.delegate loginView:self doWeChatLogin:button];
    }
}

#pragma mark - Getter
-(NSString *)getUsername{
    return _usernameTextField.text;
}

-(NSString *)getPassword{
    return _passwordTextField.text;
}

@end
